package com.zhenghaijin.searchservice.mapper;

import com.zhenghaijin.searchservice.model.Anime;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AnimeMapper {

    // 动态查询：关键字模糊搜索、分类和标签筛选，并分页
    @Select("SELECT * FROM anime " +
            "WHERE 1=1 " +
            "AND (title LIKE CONCAT('%%', #{keyword}, '%%') OR description LIKE CONCAT('%%', #{keyword}, '%%')) " +
            "AND category = #{category} " +
            "AND tags LIKE CONCAT('%%', #{tag}, '%%') " +
            "ORDER BY id DESC " +
            "LIMIT #{offset}, #{size}")
    List<Anime> search(
            @Param("keyword") String keyword,
            @Param("category") String category,
            @Param("tag") String tag,
            @Param("offset") int offset,
            @Param("size") int size);

    // 查询满足条件的总记录数
    @Select("SELECT COUNT(*) FROM anime " +
            "WHERE 1=1 " +
            "AND (title LIKE CONCAT('%%', #{keyword}, '%%') OR description LIKE CONCAT('%%', #{keyword}, '%%')) " +
            "AND category = #{category} " +
            "AND tags LIKE CONCAT('%%', #{tag}, '%%')")
    Long countByConditions(
            @Param("keyword") String keyword,
            @Param("category") String category,
            @Param("tag") String tag);

    // 清空 anime 表
    @Delete("DELETE FROM anime")
    void deleteAll();

    // 单条插入
    @Insert("INSERT INTO anime (id, title, category, tags, description) " +
            "VALUES (#{id}, #{title}, #{category}, #{tags}, #{description})")
    void insertAnime(Anime anime);
}